define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method;


    var emotion = require('./emotion.js'),
        emotion = new emotion();

    template.helper('transMsgTime', function(date) {
        return method.transMsgTime(1e3 * parseInt(date));
    });
    template.helper('formatContent', function(data) {
        return emotion.htmltranslation(data);
    });


    var textfiles = require('./textfiles.js');
    var imagefiles = require('./imagefiles.js');
    var audiofiles = require('./audiofiles.js');
    var videofiles = require('./videofiles.js');
    var articlefiles = require('./articlefiles.js');
    var filefiles = require('./filefiles.js');
    var massfooter = require("./footers/massfooter.html")


    var $js_wrap = $(".js_message_edition_wrap");
    var $js_preview = $(".js_preview");
    var $js_footer = $(".js_footer");

    window.files = {};
    window.sendConfig = {
        SafeFlag: false,
        // SessionFlag: false, 
        Type: '',
        ToDepartment: [],
        ToUser: [],
        app: window.message_config.app
    };
    for (var i = window.message_config.files_type_ids.length - 1; i >= 0; i--) {
        window.files[window.message_config.files_type_ids[i]] = {};
    };
    var elementsids = [".js_text_container", ".js_image_container", ".js_audio_container", ".js_video_container", ".js_article_container", ".js_file_container"]

    var message = function() {
        var _self = this;
        return {
            init: function(t, c) {
                _self.show(t, c);
                _self.events();

            },
            validation: function() {
                _self.model.validation();
            }
        }
    }
    message.prototype.events = function() {
        var _self = this;
        $(document).on("shown.bs.tab", '.js_message_filetype a[data-toggle="tab"]', function() {
            var $this = $(this),
                type = $this.data("type"),
                compose = $this.data("compose");
            _self.show(type, compose);
        })
        $(document).on("click", '.message_sort_title', function(e) {
            var $title = $(e.currentTarget);
            $title.next("ul").slideToggle();
            var $icon = $title.children("span.icon");
            $icon.hasClass("icon_arrow_down") ? $icon.removeClass("icon_arrow_down").addClass("icon_arrow_right") : $icon.removeClass("icon_arrow_right").addClass("icon_arrow_down")
        })
        // $(document).on("click", ".js_message_permanent", function() {
        //     window.sendConfig.SessionFlag = $(this).prop('checked');
        // })
        $(document).on("click", ".js_message_security", function() {
            var flag = $(this).prop('checked');
            window.sendConfig.SafeFlag = flag;
            if (_self.model) {
                _self.model.model.isSecurity = flag;
                _self.model.sync()
            };
        })
        var sendMsg = function(op, $this) {
            var formData = $(".js_send_form");
            var tolist = formData.tokenInput("get");
            window.sendConfig.ToUser = [];
            window.sendConfig.ToDepartment = [];
            window.sendConfig.To = formData.val();
            if (tolist) {
                for (var i = tolist.length - 1; i >= 0; i--) {
                    var itme = tolist[i];
                    if (itme.username) {
                        window.sendConfig.ToUser.push(itme)
                    } else {
                        window.sendConfig.ToDepartment.push(itme)
                    }


                };
            };
            op && (window.sendConfig = $.extend({}, window.sendConfig, op));
            var data = $.extend({}, window.sendConfig, _self.model.fileModel);
            if (!window.release) {
                console.log(data)
            };
            $this.button('loading');
            method.postd(window.message_config.send_path, function(d) {
                if (config.issucceed(d)) {
                    config.msg.suc(d.message || config.lang.modifySuccess, d.url)
                } else {
                    config.msg.error(d.message || config.lang.modifyError, d.url)
                }
                $this.button('reset');
            }, data);
        }
        $(document).on("click", ".js_message_send,.js_message_preview", function() {
            var $this = $(this),
                preview = $this.data("preview");
            var toList = $(".js_send_form").tokenInput("get");
            if (toList.length > 0) {
                _self.model.validation() && sendMsg({
                    preview: preview
                }, $this);
            } else {
                config.msg.info("请选择发送的部门或成员");
            }

        })

    };
    message.prototype.scrollevents = function() {
        var _self = this;
        this.$blcok = $(".message_block");
        this.$blcok.scrollPage("destroy");
        this.$blcok.scrollPage({
            selector: _self.model.selector,
            path: window.message_config.list_path,
            data: {
                count: 40,
                type: _self.model.type
            }
        }, function(element, result) {
            $(".js_load").remove();
            element.scrollPage("toggleDone", result.is_continue);
            _self.model.list(result);
            $.each(result.file_item, function(index, item) {
                window.files[_self.model.type][item.file_id] = item; //检索用
            });
        })
    };
    message.prototype.layout = {
        setFooter: function(html) {
            $js_footer.html(html)
        },
        setWarp: function(html) {
            if ($js_wrap.is(":hidden")) {
                $js_wrap.addClass('active').siblings().removeClass('active')
            };
            $js_wrap.html(html);
        },
        setPreview: function(html) {
            $js_preview.html(html);
        },
        hidePreview: function() {
            $js_preview.addClass('hide');
        },
        showPreview: function() {
            $js_preview.removeClass('hide');
        },
        hideFooter: function() {
            $js_footer.addClass('hide');
        },
        showFooter: function() {
            $js_footer.removeClass('hide');
        },
        setMassfooter: function(model) {
            var $html = $(massfooter);
            $js_footer.html($html);
        },
        blcokDestroy: function() {
            $(".message_block").scrollPage("destroy");
        }
    }

    message.prototype.show = function(type, compose) {
        var _self = this;
        switch (type) {
            case 0:
                _self.model = new textfiles(elementsids[type], _self.layout, compose);
                break;
            case 1:
                _self.model = new imagefiles(elementsids[type], _self.layout);
                break;
            case 2:
                _self.model = new audiofiles(elementsids[type], _self.layout);
                break;
            case 3:
                _self.model = new videofiles(elementsids[type], _self.layout);
                break;
            case 4:
                _self.model = new articlefiles(elementsids[type], _self.layout);
                break;
            case 5:
                _self.model = new filefiles(elementsids[type], _self.layout);
                break;
        };
        if (!compose) {
            $(elementsids[type]).html('<div class="empty_placeholder_wrap load js_load" ></div>')
            _self.layout.hidePreview();
            _self.scrollevents()
        }
        window.sendConfig.Type = _self.model.type;
        window.sendConfig.SafeFlag = false; //重置是否安全
    }

    var instance = new message();
    instance.init(0, true)
})
